package managers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import javax.net.ssl.HttpsURLConnection;

import structures.GamifyResult;
import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;

public class BadgeManager {
	
	private final String USER_AGENT = "Mozilla/5.0";


	
		// HTTP GET request that retrieve the available badges
		public void obtainBadges() throws Exception {
			
		System.setProperty("jsse.enableSNIExtension", "false");
			
			String url = "https://www.gamify.ws/api/v1/badges/servicetechnologies";
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
			con.setRequestMethod("GET");					
			con.setRequestProperty("User-Agent", USER_AGENT);

	 
		/*	int responseCode1 = con.getResponseCode();

			System.out.println("\nSending 'GET' request to URL : " + url);

			System.out.println("Response Code : " + responseCode1); */
			
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer responseBuffer = new StringBuffer();
			
			while ((inputLine = in.readLine()) != null) {
				responseBuffer.append(inputLine);
			}
			in.close();	
	 
			String response = responseBuffer.toString();
			//print result
			System.out.println(response.toString());
			

			}
		
		
		//HTTP post request that retrieves the user's token
		public ArrayList<GamifyResult> getUserBadges(String user_id, String schemaName) throws Exception {
					
			System.setProperty("jsse.enableSNIExtension", "false");
				
			ArrayList<GamifyResult> gamifyResults;
				JSONObject answerJson;
				String url = "https://www.gamify.ws/api/v1/badges_awarded/"+schemaName+"?user_id="+user_id;			  	 
				URL obj = new URL(url);					
				HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
				con.setRequestMethod("GET");
				con.setRequestProperty("User-Agent", USER_AGENT);

			 
				/*	int responseCode1 = con.getResponseCode();

					System.out.println("\nSending 'GET' request to URL : " + url);

					System.out.println("Response Code : " + responseCode1); */
					
			 
				BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
				String inputLine;
				StringBuffer responseBuffer = new StringBuffer();				
			 
				while ((inputLine = in.readLine()) != null) {
					responseBuffer.append(inputLine);
					}				
					in.close();	
			 
				String response = responseBuffer.toString();
						
				System.out.println(response);
				System.out.flush();
				
				
				// obtaining the badges' name
				answerJson = new JSONObject(response);
				
		 		JSONArray badgeArray = answerJson.getJSONArray("data");
				
				gamifyResults = new ArrayList<GamifyResult>();
				
				if(!badgeArray.isNull(0)){
					gamifyResults.add(new GamifyResult(badgeArray.getJSONObject(0).getString("badge_name"), getBadgeImageName(badgeArray.getJSONObject(0).getString("badge_name"), schemaName)));
					}
				
				if(!badgeArray.isNull(1)){
					gamifyResults.add(new GamifyResult(badgeArray.getJSONObject(1).getString("badge_name"), getBadgeImageName(badgeArray.getJSONObject(1).getString("badge_name"), schemaName)));
					}
				if(!badgeArray.isNull(2)){
					gamifyResults.add(new GamifyResult(badgeArray.getJSONObject(2).getString("badge_name"), getBadgeImageName(badgeArray.getJSONObject(2).getString("badge_name"), schemaName)));
					}

		 		return gamifyResults;
				}
		
		public String getBadgeImageName(String badge_name, String schemaName) throws IOException, JSONException{
					
		System.setProperty("jsse.enableSNIExtension", "false");
		
			String url = "https://www.gamify.ws/api/v1/badges/"+schemaName+"?name="+badge_name;
			JSONObject answerJson;		
			URL obj = new URL(url);			
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
			con.setRequestMethod("GET");
			con.setRequestProperty("User-Agent", USER_AGENT);

		 
			/*	int responseCode1 = con.getResponseCode();

				System.out.println("\nSending 'GET' request to URL : " + url);

				System.out.println("Response Code : " + responseCode1); */
				
		 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer responseBuffer = new StringBuffer();
						 
			while ((inputLine = in.readLine()) != null) {
				responseBuffer.append(inputLine);
				}				
				in.close();	
		 
			String response = responseBuffer.toString();
							
			// obtaining the badges' urls
			answerJson = new JSONObject(response);
	 			
			String badge_url= new String();
			JSONArray badgeArray = answerJson.getJSONArray("data");
			
			if(!badgeArray.isNull(0)){
				badge_url=badgeArray.getJSONObject(0).getString("url");
				}
			
	 		return badge_url;
			
			
		} 
		

		

}
